package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.config.poi.PoiUtils;
import com.website.core.util.DateUtil;
import com.website.system.model.FlowerType;
import com.website.system.model.Flowers;
import com.website.system.model.vo.FlowerVO;
import com.website.system.service.FlowerTypeService;
import com.website.system.service.FlowersService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("manage/flowers")
public class FlowersController extends BaseController {

    @Resource
    private FlowersService flowersService;

    @Resource
    private FlowerTypeService fTypeService;

    /**
     * 跳转到花卉花木内容页面
     * @return
     */
    @RequestMapping("")
    public String flowers(){
        return "manage/flowers";
    }

    /**
     * 分页查询花卉花木所有信息
     * @return
     */
    //@HandlingTime
    @ResponseBody
    @RequestMapping("/flowersTable")
    public ResultMap flowersTable(@RequestParam Integer page, @RequestParam Integer limit,
        @RequestParam(required = false) String name){
        ResultMap<List<FlowerVO>> map = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Page<Flowers> pageInfo = new Page<>(page,limit);
        Wrapper<Flowers> wrapper = new EntityWrapper<>();
        wrapper = name == null ? wrapper : wrapper.like("name", name);
        List<Flowers> flowers = flowersService.selectPage(pageInfo,wrapper).getRecords();
        map.setCount(flowersService.selectCount(wrapper));
        List<FlowerVO> flowerVOList = new ArrayList<>();
        for (Flowers f:flowers) {
            FlowerVO fVO = new FlowerVO();
            FlowerType fType=fTypeService.selectById(f.getFid());
            fVO.setId(f.getId());
            fVO.setName(f.getName());
            fVO.setColor(f.getColor());
            fVO.setFproperty(f.getFproperty());
            fVO.setDetail(f.getDetail());
            fVO.setImage(f.getImage());
            fVO.setFid(f.getFid());
            fVO.setFname(fType.getFname());
            flowerVOList.add(fVO);
        }
        map.setData(flowerVOList);
        return map;
    }

    //查看
    @RequestMapping("/readFlower")
    public String readFType(Model model, @RequestParam Integer id) {
        Flowers f=flowersService.selectById(id);
        model.addAttribute("flowers", f);
        model.addAttribute("fType", fTypeService.selectById(f.getFid()));
        return "manage/flowersRead";
    }

    //删除
    @ResponseBody
    @RequestMapping(value = "/delFlower", method = RequestMethod.POST)
    public String delFlower(@RequestParam Integer id){
        try {
            flowersService.deleteById(id);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }

    //跳转到修改页面
    @RequestMapping("/toFlowersUpdate")
    public String updateFType(Model model, @RequestParam Integer id) {
        Flowers f=flowersService.selectById(id);
        model.addAttribute("flowers", f);
        model.addAttribute("fType", fTypeService.selectById(f.getFid()));
        return "manage/flowersUpdate";
    }

    //修改
    @ResponseBody
    @RequestMapping("/flowersUpdate")
    public String updateFlowers(Flowers flowers){
        try {
            /*Flowers f=flowersService.selectById(flowers.getId());
            f.setName(flowers.getName());
            f.setColor(flowers.getColor());
            f.setFproperty(flowers.getFproperty());
            f.setDetail(flowers.getDetail());
            f.setImage(flowers.getImage());
            f.setFid(flowers.getFid());*/
            flowersService.updateById(flowers);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }

    //跳转到添加页面
    @RequestMapping("/toFlowersAdd")
    public String toAddFType(){
        return "manage/flowersAdd";
    }

    //添加
    @ResponseBody
    @RequestMapping("/flowersAdd")
    public String addFType(Flowers flowers){
        flowers.setImage("files/image/"+flowers.getImage()+".jpg");
        flowersService.insert(flowers);
        return "添加成功";
    }

    //上传图片
    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public ResultMap fileUpload(@RequestParam("file") MultipartFile file) {
        String imgName = DateUtil.getAllTime();
        try {
            String fileSavePath = getFileUploadPath() + "/image/" + imgName + ".jpg";
            file.transferTo(new File(fileSavePath));
            return getResultMap(SUCCESS_CODE,imgName);
        } catch (Exception e) {
            return getResultMap(ERROR_CODE,"图片上传失败，请稍后重试");
        }
    }

    @RequestMapping(value = "/exportFlowers", method = RequestMethod.GET)
    public ResponseEntity<byte[]> exportEmp() {
        return PoiUtils.exportFowers2Excel(flowersService.findAll());
    }

    //上传Excel
    @RequestMapping(value = "/importFlowers")
    public String importFlowers(MultipartFile file) {
        List<Flowers> flower = PoiUtils.importFlowersList(file);
        if (flowersService.addFlower(flower)== flower.size()) {
            return "导入成功!";
        }
        return "导入失败!";
    }

}